package com.huike.business.service.impl;


import com.huike.business.domain.TbBusiness;
import com.huike.business.domain.TbBusinessTrackRecord;
import com.huike.business.domain.vo.BusinessTrackVo;
import com.huike.business.mapper.TbBusinessMapper;
import com.huike.business.mapper.TbBusinessTrackRecordMapper;
import com.huike.business.service.ITbBusinessTrackRecordService;
import com.huike.common.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 商机跟进记录Service业务层处理
 *
 * @author wgl
 * @date 2021-04-28
 */
@Service
public class TbBusinessTrackRecordServiceImpl implements ITbBusinessTrackRecordService {
    @Autowired
    private TbBusinessTrackRecordMapper tbBusinessTrackRecordMapper;
    @Autowired
    private TbBusinessMapper businessMapper;

    @Override

    public void add(BusinessTrackVo trackVo) {
        // 1. 获取商机信息
        TbBusiness vo = businessMapper.selectTbBusinessById(trackVo.getBusinessId());
        // TbBusiness vo = new TbBusiness();
        if (vo != null) {
            // 2. 更新商机信息
            BeanUtils.copyProperties(trackVo, vo);
            vo.setUpdateTime(new Date());
            vo.setId(trackVo.getBusinessId());
            tbBusinessTrackRecordMapper.updateTbBusiness(vo);

            // 3. 创建跟进记录
            TbBusinessTrackRecord record = createTrackRecordFromVo(trackVo);
            record.setCreateTime(new Date());
            tbBusinessTrackRecordMapper.insertBusinessTrcackRecord(record);
        }
    }

    @Override
    public List<TbBusinessTrackRecord> listBusinessTrcackRecord(Long id) {
        return tbBusinessTrackRecordMapper.listBusinessTrcackRecord(id);
    }

    /**
     * 从VO创建跟进记录
     */
    private TbBusinessTrackRecord createTrackRecordFromVo(BusinessTrackVo trackVo) {
        TbBusinessTrackRecord record = new TbBusinessTrackRecord();

        // 设置基本属性
        record.setBusinessId(trackVo.getBusinessId());
        record.setTrackStatus(trackVo.getTrackStatus());
        record.setNextTime(trackVo.getNextTime());
        record.setKeyItems(trackVo.getKeyItems());
        record.setRecord(trackVo.getRecord());
        record.setType(trackVo.getType());
        record.setFalseReason(trackVo.getFalseReason());

        return record;
    }
}
